using System;
using Impact.Model.Objects;
using Impact.Controllers;
using System.Web.UI.WebControls;

namespace Impact.Web.Admin
{
    public partial class EditGalleries : Impact.Web.Foundation.PageBase
    {

        /// <summary>
        /// perform data binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            PhotoGalleryCollection galleries = PhotoController.GetAllGalleries();
            if ( galleries.Count > 0 )
            {
                galleryList.DataSource = galleries;
                galleryList.DataBind();
            }
            else
            {
                galleryList.Visible = false;
                noGalleriesPanel.Visible = true;
            }

        }

        /// <summary>
        /// event handler for when a button is clicked in the repeater
        /// </summary>
        protected void GalleryList_ItemCommand( object source, RepeaterCommandEventArgs e )
        {
            switch ( e.CommandName )
            {
                case "toggle":
                    PhotoController.ToggleVisible( e.CommandArgument.ToString() );
                    break;
                case "delete":
                    PhotoController.DeleteGallery( e.CommandArgument.ToString() );
                    break;

                default:
                    throw new ArgumentException( string.Format( "Command not found: {0}", e.CommandName ) );
            }
            Bind();
        }

        /// <summary>
        /// retrieve number of photos in the given gallery
        /// </summary>
        /// <param name="item">a PhotoGallery object to retrieve a count for</param>
        protected string GetPhotoCount( object item )
        {
            PhotoGallery gallery = item as PhotoGallery;
            if ( gallery != null )
            {
                return gallery.Photos.Count.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// event handler fired when the user clicks the add button
        /// </summary>
        protected void AddGallery_Command( object sender, CommandEventArgs e )
        {
            ToggleGalleryAdd( true );
        }

        /// <summary>
        /// event handler fired when the user submits a new gallery
        /// </summary>
        protected void NewGallerySubmit( object sender, EventArgs e )
        {
            if ( !string.IsNullOrEmpty( newGalleryName.Text ) )
            {
                PhotoController.AddGallery( newGalleryName.Text );
            }

            ToggleGalleryAdd( false );
            Bind();
        }

        /// <summary>
        /// event handler fired when the user cancels creating a new gallery
        /// </summary>
        protected void NewGalleryCancel( object sender, EventArgs e )
        {
            ToggleGalleryAdd( false );
        }

        /// <summary>
        /// shows or hides the "add gallery" form controls
        /// </summary>
        /// <param name="show">true to show the "add gallery" form controls, false to hide</param>
        private void ToggleGalleryAdd( bool show )
        {
            addGalleryButton.Visible = !show;
            newGalleryPanel.Visible = show;
            newGalleryName.Text = string.Empty;
            if ( show ) newGalleryName.Focus();
        }


    }
}
